package tt.com.warehousemanager.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import tt.com.common.enums.Status;
import tt.com.common.model.YxCommonProviders;
import tt.com.common.util.CommonResult;
import tt.com.common.util.ObjectUtil;
import tt.com.common.util.PageUtil;
import tt.com.warehousemanager.service.ProvidersService;

/**
 * @author Czycka
 * @date 2022/8/22 18:12
 * @description 供应商控制类
 */
@RestController
@RequestMapping(value = "warehouse/provider")
public class ProvidersServiceController {

    @Autowired
    private ProvidersService service;

    @PostMapping(value = "insert")
    public CommonResult insert(@RequestBody YxCommonProviders purchase) {
        if (!ObjectUtil.objectIsNull(purchase)) {
            return CommonResult.buildR(Status.PARAM_ISNULL, "参数为空");
        }
        Integer result = service.insertProvider(purchase, "2");
        if (result == -1) {
            return CommonResult.buildR(Status.SYSTEM_ERROR, "不允许重复添加");
        } else if (result > 0) {
            return CommonResult.buildR(Status.OK, "供应商添加成功");
        } else {
            return CommonResult.buildR(Status.SYSTEM_ERROR, "供应商添加失败");
        }
    }

    @PostMapping(value = "delete/{id}")
    public CommonResult delete(@PathVariable("id") Long id) {
        if (id == null) {
            return CommonResult.buildR(Status.PARAM_ISNULL, "参数为空");
        }
        Integer result = service.deleteProvider(id, "2");
        if (result == -1) {
            return CommonResult.buildR(Status.SYSTEM_ERROR, "供应商不存在");
        } else if (result == -2) {
            return CommonResult.buildR(Status.SYSTEM_ERROR, "供应商已删除");
        } else {
            return CommonResult.buildR(Status.SYSTEM_ERROR, "供应商删除成功");
        }
    }

    @PostMapping(value = "update")
    public CommonResult update(@RequestBody YxCommonProviders purchase) {
        if (purchase == null) {
            return CommonResult.buildR(Status.PARAM_ISNULL, "参数为空");
        }
        if (service.updateProvider(purchase, "2") > 0) {
            return CommonResult.buildR(Status.OK, "供应商修改成功");
        } else {
            return CommonResult.buildR(Status.SYSTEM_ERROR, "供应商修改失败");
        }
    }

    @PostMapping(value = "show/page")
    public CommonResult showPage(@RequestBody PageUtil pageUtil) {
        if (pageUtil.getPageIndex() == null || pageUtil.getPageSize() == null) {
            return CommonResult.buildR(Status.PARAM_ISNULL, "参数为空");
        }
        pageUtil = service.showProviderByPage(pageUtil);
        return CommonResult.buildR(Status.OK, "查询成功", pageUtil);
    }

    @PostMapping(value = "show/all")
    public CommonResult showAll() {
        return CommonResult.buildR(Status.OK, "查询成功", service.list());
    }

    @PostMapping(value = "show/{id}")
    public CommonResult showById(@PathVariable("id") Long id) {
        if (id == null) {
            return CommonResult.buildR(Status.PARAM_ISNULL, "参数为空");
        }
        return CommonResult.buildR(Status.OK, "查询成功", service.getById(id));
    }

}
